package com.sgb.contract.view.ui.details;

import android.view.View;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.sgb.contract.BR;
import com.sgb.contract.R;
import com.sgb.contract.base.BaseActivity;
import com.sgb.contract.databinding.ContractActivitySignContractBinding;
import com.sgb.contract.utils.ArouterConfigs;
import com.sgb.contract.view.widget.pop.RejectContractPop;
import com.swgk.core.recyclerview.decoration.RecyclerCustomDecoration;
import com.swgk.core.util.MToast;
import com.swgk.core.util.UiUtil;

import org.jetbrains.annotations.NotNull;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

/**
 * 签署合同
 * 业务：
 * 角色分为：参与方和发起方
 * 流   程：发起方拟定合同并发送合同，接收方接受合同并签署，最后发起方确认并签署合同
 *
 * @author cyj
 */
@Route(path = ArouterConfigs.AR_SIGN_CONTRACT)
public class SignContractActivity extends BaseActivity<ContractActivitySignContractBinding, SignContractViewModel> {

    private int positionProportionSize = 0;
    private RejectContractPop rejectContractPop;

    @Override
    protected int getLayoutResId() {
        return R.layout.contract_activity_sign_contract;
    }

    @Override
    protected void initViewModel() {
        mViewModel = createViewModel(SignContractViewModel.class, BR.mSignContractViewModel);
    }

    @Override
    protected void init() {
        // head layout设置
        mDataBinding.head.title.setText(R.string.name_title_contract_detail);
        mDataBinding.head.right.setTextColor(getResources().getColor(R.color.color_ffb79255));
        mDataBinding.head.right.setText(R.string.other_info_contract_detail);

        if (null == getIntent().getExtras()) {
            finish();
            return;
        }
        mDataBinding.contractRlv.post(() -> positionProportionSize = (int) (mDataBinding.contractRlv.getHeight() * 0.4));

        mDataBinding.contractRlv.addItemDecoration(new RecyclerCustomDecoration(0, 0, 0,
                getResources().getDimension(R.dimen.dp5), getResources().getColor(R.color.color_ffededed)));

        if (null != getIntent().getExtras()) {
            // 查询合同详情
            mViewModel.getContractDetail(getIntent().getExtras().getString("contractId", ""));
            mViewModel.showOperationLayout.set(getIntent().getExtras().getBoolean("isShowOperationLayout", true));
        }
    }

    @Override
    protected void initListener() {
        mDataBinding.head.ivBack.setOnClickListener(v -> finish());
        mDataBinding.head.right.setOnClickListener(v ->
                {
                    if (mViewModel.bean == null || mViewModel.bean.status == null) {
                        MToast.showToast(UiUtil.getContext(), "数据异常");
                        return;
                    }
                    ARouter.getInstance().build(ArouterConfigs.AR_CONTRACT_OTHER_INFO)
                            .withString("contractNo", mViewModel.bean.no)
                            .withString("contractId", String.valueOf(mViewModel.bean.id))
                            .withString("companyNo", mViewModel.bean.companyNo)
                            .withString("auditId", mViewModel.bean.auditId)
                            .withString("status", mViewModel.bean.status)
                            .navigation();
                }
        );
        mDataBinding.contractRlv.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(@NonNull @NotNull RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                // 计算当前item滑动位置，并更新UI
                mViewModel.calcCurrentPosition(recyclerView, positionProportionSize);
            }
        });
        mDataBinding.layoutValidationCode.imgClose.setOnClickListener(v -> {
            hideSoftInputFromWindow(mDataBinding.layoutValidationCode.etValidationCode);
            mViewModel.closeGetValidationCodeLayout();
        });
        mDataBinding.layoutValidationCode.imgClose1.setOnClickListener(v -> {
            mViewModel.closeGetValidationCodeLayout();
            mViewModel.lookContractDetails();
        });
    }

    @Override
    protected void initObserve() {
        mViewModel.showNoDataView.observe(this, obj -> {
            mViewModel.showNoData(mDataBinding.flContainer);
        });
        mViewModel.showRejectContractPop.observe(this, b -> {
            if (null == rejectContractPop) {
                rejectContractPop = new RejectContractPop(SignContractActivity.this);
            }
            rejectContractPop.showPopwindow(mDataBinding.grayLayout, true);
            rejectContractPop.setRejectContractListener(obj -> {
                // 修改合同
                mViewModel.rejectContractSure(obj);
            });
        });
    }

    @Override
    protected void onDialogOnClick(View dialogView) {
        if ("timeFailure".equals(dialogView.getTag().toString())) {
            // 根据合同截止日期判断合同已过期，并显示查看合同，即隐藏操作layout视图
            mDataBinding.llB.setVisibility(View.GONE);
            // 查询合同详情
            mViewModel.getContractDetail(getIntent().getExtras().getString("contractId", ""));
        } else if ("getValidationCode".equals(dialogView.getTag().toString())) {
            // 获取验证码弹窗，则点击知道了，关闭获取验证码弹窗窗口UI
            mViewModel.showValidationCodeLayout.set(false);
        } else if ("discardContract".equals(dialogView.getTag().toString())) {
            mViewModel.discardContract();
        } else if ("checkStatusByNowStatus".equals(dialogView.getTag().toString())) {

            mViewModel.lookContractDetails();
        } else if ("findAuditStatus".equals(dialogView.getTag().toString())) {
            //审批状态发生变更，刷新页面
            mViewModel.getContractDetail(getIntent().getExtras().getString("contractId", ""));
        } else if ("checkApprovalOpenAudit".equals(dialogView.getTag().toString())) {
            //审批开启状态发生变更，刷新页面
            mViewModel.getContractDetail(getIntent().getExtras().getString("contractId", ""));
        } else if ("backStatus".equals(dialogView.getTag().toString())) {
            // 弹框点击退回
            // TODO: 2021/12/1 0001
            mViewModel.operationContractOnClick();
        }
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        mViewModel.showOperationLayout.set(false);
        mViewModel.adapter.removeAllChapter();
        mViewModel.adapter.setDatas(null);
        mViewModel.adapter.notifyDataSetChanged();
        mViewModel.maxChapterNum.set(mViewModel.MAX_NUM);
        mViewModel.getContractDetail(getIntent().getExtras().getString("contractId", ""));
    }
}
